const mysql = require('mysql2/promise');

// 数据库连接配置
const dbConfig = {
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'community_service_platform',
  port: 3306
};

async function debugSharedItems() {
  try {
    console.log('🔍 正在检查shared_items表...');
    
    // 创建连接
    const connection = await mysql.createConnection(dbConfig);
    console.log('✅ 数据库连接成功！');
    
    // 检查表是否存在
    const [tables] = await connection.execute('SHOW TABLES LIKE "shared_items"');
    if (tables.length === 0) {
      console.log('❌ shared_items表不存在');
      
      // 查看所有表
      const [allTables] = await connection.execute('SHOW TABLES');
      console.log('📋 数据库中的表:');
      allTables.forEach(table => {
        const tableName = Object.values(table)[0];
        console.log(`   - ${tableName}`);
      });
      
      await connection.end();
      return;
    }
    
    console.log('✅ shared_items表存在');
    
    // 查看表结构
    console.log('\n📝 shared_items表结构:');
    const [columns] = await connection.execute('DESCRIBE shared_items');
    columns.forEach(col => {
      console.log(`   - ${col.Field}: ${col.Type} ${col.Null === 'YES' ? '(可空)' : '(非空)'} ${col.Key ? `[${col.Key}]` : ''}`);
    });
    
    // 查看数据
    console.log('\n📊 shared_items表数据:');
    const [rows] = await connection.execute('SELECT * FROM shared_items LIMIT 5');
    console.log(`找到 ${rows.length} 条记录:`);
    rows.forEach((row, index) => {
      console.log(`\n记录 ${index + 1}:`);
      Object.entries(row).forEach(([key, value]) => {
        console.log(`   ${key}: ${value} (类型: ${typeof value})`);
      });
    });
    
    await connection.end();
    console.log('\n✅ 调试完成！');
    
  } catch (error) {
    console.error('❌ 调试失败:', error.message);
  }
}

debugSharedItems();
